import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/Login.vue'
import {getToken} from "../utils/auth";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: HomeView,
        },
        {
            path: '/Login',
            name: 'Login',
            component: () => import('../views/Login.vue'),
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('../views/Register.vue'),
        },
        {
            path: '/forget',
            name: 'forget',
            component: () => import('../views/forget.vue'),
        },
        {
            path: '/main',
            name: 'main',
            component: () => import('../views/main/index.vue'),
            children:[
                {path: '/leader',component: () => import('../views/main/leader.vue')},
                {path: '/teacher',component: () => import('../views/teacher/index.vue')},
                {path: '/student',component: () => import('../views/student/index.vue')},
                {path: '/setting',component: () => import('../views/setting/index.vue')},
                {path: '/notice',component: () => import('../views/notice/index.vue')},
                {path: '/health',component: () => import('../views/Health/index.vue')},
                {path: '/test',component: () => import('../views/test/index.vue')},
                {path: '/stress',component: () => import('../views/stress/index.vue')},
                {path: '/doctor',component: () => import('../views/doctor/index.vue')},
                {path: '/comments',component: () => import('../views/comments/index.vue')},
                {path: '/Appointment',component: () => import('../views/Appointment/index.vue')},
                {path: '/doctor/details',component: () => import('../views/doctor/details.vue')},
                {path: '/user',component: () => import('../views/user/index.vue')},
            ]
        },
    ],
})


// 路由白名单
const whiteList=['/Login','/register', '/forget']
// GOOD
router.beforeEach((to, from, next) => {
    const isAuthenticated = getToken("token")
    if(whiteList.indexOf(to.path) !== -1){
        console.log("白名单")
        next()
        return
    }
    if (to.name !== 'Login' && !isAuthenticated) next({name: 'Login'})
    else next()
})




export default router
